package com.gimi.cloud.bbp.app.consumer.service.impl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.gimi.cloud.bbp.app.consumer.feign.RegisterFeignService;
import com.gimi.cloud.bbp.app.consumer.service.RegisterService;
import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.JsonUtil;
import com.gimi.cloud.bbp.common.utils.NumberUtil;
import com.gimi.cloud.bbp.common.utils.StringUtil;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.common.utils.reqhead.Util;
import com.gimi.cloud.bbp.dto.user.UserDTO;

/**
 * Created by shazhiwei on 2017/10/17.
 */
@Service
public class RegisterServiceImpl implements RegisterService {

    private static Logger logger = LoggerFactory.getLogger(RegisterServiceImpl.class);
    private RegisterFeignService registerFeignService;

    public RegisterServiceImpl(RegisterFeignService registerFeignService){
        this.registerFeignService = registerFeignService;
    }

    @Override
    public void checkPhoneAvailable(String body, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) {
        logger.info("srcData:" + body);
        UserDTO params = JsonUtil.jsonToBean(body,UserDTO.class);
        String phone = params.getMobilePhone();
        if(StringUtil.isBlank(phone)){
            Response response = new Response();
            response.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR);
            Util.sendResponse(JsonUtil.toJson(response),httpServletResponse);
            return;
        }
        Util.sendResponse(registerFeignService.checkPhoneAvailable(phone), httpServletResponse);
    }

    @Override
    public void doRegister(String body, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) {
        logger.info("srcData:" + body);
        UserDTO params = JsonUtil.jsonToBean(body,UserDTO.class);
        if(params == null || StringUtil.isBlank(params.getCode()) ||
                StringUtil.isBlank(params.getMobilePhone()) || StringUtil.isBlank(params.getPwd())){
            Response response = new Response();
            response.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR);
            Util.sendResponse(JsonUtil.toJson(response),httpServletResponse);
            return;
        }
        Util.sendResponse(registerFeignService.doRegister(params), httpServletResponse);
    }

    @Override
    public void doLoginByPassword(String body, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) {
        logger.info("srcData:" + body);
        UserDTO params = JsonUtil.jsonToBean(body,UserDTO.class);
        if(params == null){
            Response response = new Response();
            response.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR);
            Util.sendResponse(JsonUtil.toJson(response),httpServletResponse);
            return;
        }
        if(StringUtil.isBlank(params.getMobilePhone())){
            Response response = new Response();
            response.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR);
            response.setRes_msg("手机号不能为空");
            Util.sendResponse(JsonUtil.toJson(response),httpServletResponse);
            return;
        }
        if(StringUtil.isBlank(params.getPwd())){
            Response response = new Response();
            response.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR);
            response.setRes_msg("密码不能为空");
            Util.sendResponse(JsonUtil.toJson(response),httpServletResponse);
            return;
        }
        Util.sendResponse(registerFeignService.doLoginByPassword(params), httpServletResponse);
    }


    @Override
    public void refreshToken(String body, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) {
        logger.info("srcData:" + body);
        UserDTO params = JsonUtil.jsonToBean(body,UserDTO.class);
        if(params == null || StringUtil.isBlank(params.getMobilePhone())){
            Response response = new Response();
            response.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR);
            Util.sendResponse(JsonUtil.toJson(response),httpServletResponse);
            return;
        }
        String userId = httpServletRequest.getHeader("userId");//这里到时候需要修改下
        params.setUserId(NumberUtil.getInt(userId));
        Util.sendResponse(registerFeignService.refreshToken(params), httpServletResponse);
    }

    @Override
    public void getInitPwdCode(String body, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) {
        logger.info("srcData:" + body);
        UserDTO params = JsonUtil.jsonToBean(body,UserDTO.class);
        if(params == null || StringUtil.isBlank(params.getMobilePhone())){
            Response response = new Response();
            response.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR);
            Util.sendResponse(JsonUtil.toJson(response),httpServletResponse);
            return;
        }
        Util.sendResponse(registerFeignService.getResetPwdCode(params), httpServletResponse);
    }

    @Override
    public void doLoginByPwdCode(String body, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) {
        logger.info("srcData:" + body);
        UserDTO userDTO = JsonUtil.jsonToBean(body,UserDTO.class);
        Response response = new Response();
        if(null == userDTO){
            response.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR);
            Util.sendResponse(JsonUtil.toJson(response),httpServletResponse);
            return;
        }
        if(StringUtil.isBlank(userDTO.getMobilePhone())){
            response.setResponseCode(ResponseCodeEnum.MOBILE_ERROR);
            Util.sendResponse(JsonUtil.toJson(response),httpServletResponse);
            return;
        }
        if(StringUtil.isBlank(userDTO.getCode())){
            logger.info("验证码为空");
            response.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR);
            response.setRes_msg("验证码为空");
            Util.sendResponse(JsonUtil.toJson(response),httpServletResponse);
            return;
        }
        Util.sendResponse(registerFeignService.doLoginByPwdCode(userDTO), httpServletResponse);
    }

    @Override
    public void resetPwd(String body, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) {
        logger.info("srcData:" + body);
        UserDTO params = JsonUtil.jsonToBean(body,UserDTO.class);
        if(params == null || StringUtil.isBlank(params.getMobilePhone()) || StringUtil.isBlank(params.getPwd())){
            Response response = new Response();
            response.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR);
            Util.sendResponse(JsonUtil.toJson(response),httpServletResponse);
            return;
        }
        Util.sendResponse(registerFeignService.resetPwd(params), httpServletResponse);
    }

    @Override
    public void modifyPwd(String body, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("srcData:" + body);
        UserDTO userDTO = JsonUtil.jsonToBean(body,UserDTO.class);
        if(userDTO != null){
            String userId = httpServletRequest.getHeader("userId");
            userDTO.setUserId(NumberUtil.getInt(userId));
        }

        if(userDTO == null || StringUtil.isBlank(userDTO.getMobilePhone()) || StringUtil.isBlank(userDTO.getOld_pwd()) || StringUtil.isBlank(userDTO.getNew_pwd()) || StringUtil.isBlank(userDTO.getUserId())){
            Response response = new Response();
            response.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR);
            Util.sendResponse(JsonUtil.toJson(response),httpServletResponse);
            return;
        }
        Util.sendResponse(registerFeignService.modifyPwd(userDTO), httpServletResponse);
    }

    @Override
    public void unreadMsg(String body, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) {
        logger.info("srcData:" + body);
        Long userId = NumberUtil.getLong(httpServletRequest.getHeader("userId"));
        if(userId != null && userId != 0){
            Util.sendResponse(registerFeignService.unreadMsg(userId), httpServletResponse);
        }else{
            Response response = new Response();
            response.setResponseCode(ResponseCodeEnum.ERR_USER_INVALID);
            Util.sendResponse(JsonUtil.toJson(response),httpServletResponse);
        }
    }

    @Override
    public void weChatRegister(String body, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) {
        logger.info("srcData:" + body);
        Long userId = NumberUtil.getLong(httpServletRequest.getHeader("userId"));
        if(userId != null && userId != 0){
            Util.sendResponse(registerFeignService.weChatRegister(userId), httpServletResponse);
        }else{
            Response response = new Response();
            response.setResponseCode(ResponseCodeEnum.ERR_USER_INVALID);
            Util.sendResponse(JsonUtil.toJson(response),httpServletResponse);
        }
    }

    @Override
    public void initPwd(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest request) {
        logger.info("srcData:" + requestData);
        UserDTO userDTO = JsonUtil.jsonToBean(requestData,UserDTO.class);
        if(userDTO == null || StringUtils.isBlank(userDTO.getMobilePhone())||StringUtils.isBlank(userDTO.getPwd())){
            Response response = new Response();
            response.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR);
            Util.sendResponse(JsonUtil.toJson(response),httpServletResponse);
            return;
        }
        Util.sendResponse(registerFeignService.initPassword(userDTO), httpServletResponse);
    }

    @Override
    public void checkModifypassword(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest request) {
        logger.info("srcData:" + requestData);
        UserDTO userDTO = JsonUtil.jsonToBean(requestData,UserDTO.class);
        if(userDTO == null){
            Response response = new Response();
            response.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR);
            Util.sendResponse(JsonUtil.toJson(response),httpServletResponse);
            return;
        }
        if(StringUtil.isBlank(userDTO.getMobilePhone())){
            Response response = new Response();
            response.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR);
            response.setRes_msg("手机号不能为空");
            Util.sendResponse(JsonUtil.toJson(response),httpServletResponse);
            return;
        }
        if(StringUtil.isBlank(userDTO.getCode())){
            Response response = new Response();
            response.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR);
            response.setRes_msg("验证码不能为空");
            Util.sendResponse(JsonUtil.toJson(response),httpServletResponse);
            return;
        }
        Util.sendResponse(registerFeignService.checkModifypassword(userDTO), httpServletResponse);
    }

}
